package com.freexiaoyu.cloud.ui.popupwindow;

import android.app.Activity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;

import com.freexiaoyu.cloud.R;
import com.freexiaoyu.cloud.ui.base.BaseCloudAdapter;



public class AdapterPopupWindow<T> extends BasePopupWindow {

    AppCompatTextView tv_title;
    RecyclerView rv_list;
    AppCompatImageView iv_close;
    AppCompatTextView tv_right_text;

    private View popupView;
    String strTile;
    int rightIcon;
    String rightTitle;

    protected IOnClickListener onClickListener;
    BaseCloudAdapter<T> adapter;

    public AdapterPopupWindow(Activity context, BaseCloudAdapter<T> adapter, String strTile, int rightIcon, String rightTitle) {
        super(context);
        this.adapter = adapter;
        this.strTile = strTile;
        this.rightIcon = rightIcon;
        this.rightTitle = rightTitle;
        bindEvent();
        setData();
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(
                R.layout.popup_goods_details_item, null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return null;
    }

    @Override
    protected Animation getShowAnimation() {
        return null;
    }

    @Override
    protected View getClickToDismissView() {
        return null;
    }

    private void bindEvent() {
        if (popupView != null) {
            tv_title.setText(strTile);

            tv_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            iv_close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            if (TextUtils.isEmpty(rightTitle)) {
                tv_right_text.setVisibility(View.GONE);
                iv_close.setBackgroundResource(R.mipmap.ico_manager_cart_right_delete);
            } else {
                tv_right_text.setVisibility(View.VISIBLE);
                tv_right_text.setText(rightTitle);
                tv_right_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickListener != null) {
                            onClickListener.OnClickListener(v, 0, "");
                        }
                    }
                });
            }
            popupView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

    }


    private void setData() {
        rv_list.setItemAnimator(new DefaultItemAnimator());
        rv_list.setLayoutManager(new LinearLayoutManager(mContext));
        rv_list.setAdapter(adapter);
    }


    public interface IOnClickListener {
        void OnClickListener(View view, int index, Object obj);
    }

    /***
     * 点击事件方法
     *
     * @param OnClickListener
     */
    public void setOnClickListener(IOnClickListener OnClickListener) {
        this.onClickListener = OnClickListener;
    }

}